/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.CustomTableModel;
import com.railway.utils.CustomScrollBarUI;
import com.railway.utils.Toast;
import com.railway.utils.RestServer;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class DiscountPanel extends javax.swing.JPanel {

    private Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private ArrayList<String> colName;
    private ArrayList<ArrayList<Object>> rowData;
    private CustomTableModel ctmModel;
    private JSONObject json;
    private JSONArray jsonArray;
    private CardLayout cl;
    private Integer did;

    /**
     * Creates new form RoutePanel
     */
    public DiscountPanel() {
        initComponents();
        cl = (CardLayout) jpDiscountCard.getLayout();
        jspTableScroller.getViewport().setBackground(Color.WHITE);
        populateTableData();
    }

    private void populateTableData() {
        new Thread() {
            @Override
            public void run() {
                colName = new ArrayList<>();
                colName.add("Discount ID");
                colName.add("Description");
                colName.add("Discount Rate");
                colName.add("Start Date");
                colName.add("Expire Date");
                String data = RestServer.httpGet("discount", "");
                try {
                    json = new JSONObject(data);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int status = json.getInt("status");
                        if (status != 404) {
                            rowData = new ArrayList<>();
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ArrayList<Object> temp = new ArrayList<>();
                                temp.add(new Integer(jsonArray.getJSONObject(i).getInt("discountId")));
                                temp.add(jsonArray.getJSONObject(i).getString("type"));
                                temp.add(new Double(jsonArray.getJSONObject(i).getDouble("rate")));
                                temp.add(jsonArray.getJSONObject(i).getString("startDate"));
                                temp.add(jsonArray.getJSONObject(i).getString("endDate"));
                                rowData.add(temp);
                            }
                            ctmModel = new CustomTableModel(colName, rowData);
                            jtbDiscount.setModel(ctmModel);
                            jtbDiscount.removeColumn(jtbDiscount.getColumnModel().getColumn(0));
                        }
                    } else {
                        Toast.makeText(getParent(), json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpDiscount = new javax.swing.JPanel();
        jpDiscountContent = new javax.swing.JPanel();
        jpWorkContent = new javax.swing.JPanel();
        jpDiscountCard = new javax.swing.JPanel();
        jpNullcard = new javax.swing.JPanel();
        jpAddCard = new javax.swing.JPanel();
        jpAddArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };
        jpAddDiscount = new javax.swing.JPanel();
        jlblDesc = new javax.swing.JLabel();
        jtfDesc = new javax.swing.JTextField();
        jlblRate = new javax.swing.JLabel();
        jspRate = new javax.swing.JSpinner();
        jlblExpireDate = new javax.swing.JLabel();
        jdcExpireDate = new com.toedter.calendar.JDateChooser();
        jbtClear = new javax.swing.JButton();
        jbtSubmit = new javax.swing.JButton();
        jlblStartDate = new javax.swing.JLabel();
        jdcStartDate = new com.toedter.calendar.JDateChooser();
        jpModifyCard = new javax.swing.JPanel();
        jpModifyArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };
        jpModifyDiscount = new javax.swing.JPanel();
        jbtMClear = new javax.swing.JButton();
        jbtMSubmit = new javax.swing.JButton();
        jtfMDesc = new javax.swing.JTextField();
        jlblMDesc = new javax.swing.JLabel();
        jlblMRate = new javax.swing.JLabel();
        jspMRate = new javax.swing.JSpinner();
        jdcMStartDate = new com.toedter.calendar.JDateChooser();
        jlblMStartDate = new javax.swing.JLabel();
        jlblMExpireDate = new javax.swing.JLabel();
        jdcMExpireDate = new com.toedter.calendar.JDateChooser();
        jbtAddDiscount = new javax.swing.JButton();
        jbtModifyDiscount = new javax.swing.JButton();
        jspTableScroller = new javax.swing.JScrollPane();
        jtbDiscount = new javax.swing.JTable();
        jbtRefresh = new javax.swing.JButton();
        jbtRemove = new javax.swing.JButton();
        jlblPromotion = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jpDiscountArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), getHeight(), this);
            }
        };

        setPreferredSize(new java.awt.Dimension(870, 460));
        setLayout(null);

        jpDiscount.setBackground(new java.awt.Color(220, 220, 220));
        jpDiscount.setLayout(null);

        jpDiscountContent.setBackground(new java.awt.Color(255, 255, 255));
        jpDiscountContent.setLayout(null);

        jpWorkContent.setBackground(new java.awt.Color(255, 255, 255));
        jpWorkContent.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpWorkContent.setLayout(null);

        jpDiscountCard.setBackground(new java.awt.Color(255, 255, 255));
        jpDiscountCard.setLayout(new java.awt.CardLayout());

        jpNullcard.setBackground(new java.awt.Color(255, 255, 255));
        jpNullcard.setLayout(null);
        jpDiscountCard.add(jpNullcard, "null");

        jpAddCard.setBackground(new java.awt.Color(255, 255, 255));
        jpAddCard.setLayout(null);

        jpAddArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpAddArrowLayout = new javax.swing.GroupLayout(jpAddArrow);
        jpAddArrow.setLayout(jpAddArrowLayout);
        jpAddArrowLayout.setHorizontalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 160, Short.MAX_VALUE)
        );
        jpAddArrowLayout.setVerticalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpAddCard.add(jpAddArrow);
        jpAddArrow.setBounds(0, 0, 160, 10);

        jpAddDiscount.setBackground(new java.awt.Color(255, 255, 255));
        jpAddDiscount.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpAddDiscount.setLayout(null);

        jlblDesc.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblDesc.setText("Description");
        jpAddDiscount.add(jlblDesc);
        jlblDesc.setBounds(10, 30, 110, 30);

        jtfDesc.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jpAddDiscount.add(jtfDesc);
        jtfDesc.setBounds(120, 30, 240, 30);

        jlblRate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblRate.setText("Promotion Rate");
        jpAddDiscount.add(jlblRate);
        jlblRate.setBounds(10, 75, 110, 30);

        jspRate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jspRate.setModel(new javax.swing.SpinnerNumberModel(0.1d, 0.1d, 1.0d, 0.1d));
        JFormattedTextField jftfSpinner = ((JSpinner.DefaultEditor)jspRate.getEditor()).getTextField();
        jftfSpinner.setEditable(false);
        jftfSpinner.setHorizontalAlignment(JFormattedTextField.CENTER);
        jpAddDiscount.add(jspRate);
        jspRate.setBounds(120, 75, 240, 30);

        jlblExpireDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblExpireDate.setText("Expire Date");
        jpAddDiscount.add(jlblExpireDate);
        jlblExpireDate.setBounds(10, 160, 110, 30);

        jdcExpireDate.setDateFormatString("yyyy-MM-dd");
        jdcExpireDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jdcExpireDate.setMaxSelectableDate(new java.util.Date(253370739664000L));
        jdcExpireDate.setMinSelectableDate(new Date());
        jpAddDiscount.add(jdcExpireDate);
        jdcExpireDate.setBounds(120, 160, 240, 30);

        jbtClear.setBackground(new java.awt.Color(39, 174, 96));
        jbtClear.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtClear.setForeground(new java.awt.Color(255, 255, 255));
        jbtClear.setText("Clear");
        jbtClear.setFocusPainted(false);
        jbtClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtClearActionPerformed(evt);
            }
        });
        jpAddDiscount.add(jbtClear);
        jbtClear.setBounds(10, 290, 140, 45);

        jbtSubmit.setBackground(new java.awt.Color(39, 174, 96));
        jbtSubmit.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtSubmit.setForeground(new java.awt.Color(255, 255, 255));
        jbtSubmit.setText("Submit");
        jbtSubmit.setFocusPainted(false);
        jbtSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSubmitActionPerformed(evt);
            }
        });
        jpAddDiscount.add(jbtSubmit);
        jbtSubmit.setBounds(230, 290, 140, 45);

        jlblStartDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblStartDate.setText("Start Date");
        jpAddDiscount.add(jlblStartDate);
        jlblStartDate.setBounds(10, 120, 110, 30);

        jdcStartDate.setDateFormatString("yyyy-MM-dd");
        jdcStartDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jdcStartDate.setMaxSelectableDate(new java.util.Date(253370739664000L));
        jdcStartDate.setMinSelectableDate(new Date());
        jpAddDiscount.add(jdcStartDate);
        jdcStartDate.setBounds(120, 120, 240, 30);

        jpAddCard.add(jpAddDiscount);
        jpAddDiscount.setBounds(0, 10, 380, 350);

        jpDiscountCard.add(jpAddCard, "addcard");

        jpModifyCard.setBackground(new java.awt.Color(255, 255, 255));
        jpModifyCard.setLayout(null);

        jpModifyArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpModifyArrowLayout = new javax.swing.GroupLayout(jpModifyArrow);
        jpModifyArrow.setLayout(jpModifyArrowLayout);
        jpModifyArrowLayout.setHorizontalGroup(
            jpModifyArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 160, Short.MAX_VALUE)
        );
        jpModifyArrowLayout.setVerticalGroup(
            jpModifyArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpModifyCard.add(jpModifyArrow);
        jpModifyArrow.setBounds(220, 0, 160, 10);

        jpModifyDiscount.setBackground(new java.awt.Color(255, 255, 255));
        jpModifyDiscount.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpModifyDiscount.setLayout(null);

        jbtMClear.setBackground(new java.awt.Color(39, 174, 96));
        jbtMClear.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtMClear.setForeground(new java.awt.Color(255, 255, 255));
        jbtMClear.setText("Clear");
        jbtMClear.setFocusPainted(false);
        jbtMClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMClearActionPerformed(evt);
            }
        });
        jpModifyDiscount.add(jbtMClear);
        jbtMClear.setBounds(10, 290, 140, 45);

        jbtMSubmit.setBackground(new java.awt.Color(39, 174, 96));
        jbtMSubmit.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtMSubmit.setForeground(new java.awt.Color(255, 255, 255));
        jbtMSubmit.setText("Submit");
        jbtMSubmit.setFocusPainted(false);
        jbtMSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMSubmitActionPerformed(evt);
            }
        });
        jpModifyDiscount.add(jbtMSubmit);
        jbtMSubmit.setBounds(230, 290, 140, 45);

        jtfMDesc.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jpModifyDiscount.add(jtfMDesc);
        jtfMDesc.setBounds(120, 30, 240, 30);

        jlblMDesc.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMDesc.setText("Description");
        jpModifyDiscount.add(jlblMDesc);
        jlblMDesc.setBounds(10, 30, 110, 30);

        jlblMRate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMRate.setText("Promotion Rate");
        jpModifyDiscount.add(jlblMRate);
        jlblMRate.setBounds(10, 75, 110, 30);

        jspMRate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jspMRate.setModel(new javax.swing.SpinnerNumberModel(0.1d, 0.1d, 1.0d, 0.1d));
        JFormattedTextField jftfMSpinner = ((JSpinner.DefaultEditor)jspMRate.getEditor()).getTextField();
        jftfMSpinner.setEditable(false);
        jftfMSpinner.setHorizontalAlignment(JFormattedTextField.CENTER);
        jpModifyDiscount.add(jspMRate);
        jspMRate.setBounds(120, 75, 240, 30);

        jdcMStartDate.setDateFormatString("yyyy-MM-dd");
        jdcMStartDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jdcMStartDate.setMaxSelectableDate(new java.util.Date(253370739664000L));
        jdcMStartDate.setMinSelectableDate(new Date());
        jpModifyDiscount.add(jdcMStartDate);
        jdcMStartDate.setBounds(120, 120, 240, 30);

        jlblMStartDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMStartDate.setText("Start Date");
        jpModifyDiscount.add(jlblMStartDate);
        jlblMStartDate.setBounds(10, 120, 110, 30);

        jlblMExpireDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMExpireDate.setText("Expire Date");
        jpModifyDiscount.add(jlblMExpireDate);
        jlblMExpireDate.setBounds(10, 160, 110, 30);

        jdcMExpireDate.setDateFormatString("yyyy-MM-dd");
        jdcMExpireDate.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jdcMExpireDate.setMaxSelectableDate(new java.util.Date(253370739664000L));
        jdcMExpireDate.setMinSelectableDate(new Date());
        jpModifyDiscount.add(jdcMExpireDate);
        jdcMExpireDate.setBounds(120, 160, 240, 30);

        jpModifyCard.add(jpModifyDiscount);
        jpModifyDiscount.setBounds(0, 10, 380, 350);

        jpDiscountCard.add(jpModifyCard, "modifycard");

        jpWorkContent.add(jpDiscountCard);
        jpDiscountCard.setBounds(5, 55, 380, 360);

        jbtAddDiscount.setBackground(new java.awt.Color(52, 73, 94));
        jbtAddDiscount.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtAddDiscount.setForeground(new java.awt.Color(255, 255, 255));
        jbtAddDiscount.setText("Add Discount");
        jbtAddDiscount.setFocusPainted(false);
        jbtAddDiscount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddDiscountActionPerformed(evt);
            }
        });
        jpWorkContent.add(jbtAddDiscount);
        jbtAddDiscount.setBounds(5, 5, 160, 50);

        jbtModifyDiscount.setBackground(new java.awt.Color(52, 73, 94));
        jbtModifyDiscount.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtModifyDiscount.setForeground(new java.awt.Color(255, 255, 255));
        jbtModifyDiscount.setText("Edit Discount");
        jbtModifyDiscount.setFocusPainted(false);
        jbtModifyDiscount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtModifyDiscountActionPerformed(evt);
            }
        });
        jpWorkContent.add(jbtModifyDiscount);
        jbtModifyDiscount.setBounds(225, 5, 160, 50);

        jpDiscountContent.add(jpWorkContent);
        jpWorkContent.setBounds(5, 5, 390, 420);

        jspTableScroller.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jtbDiscount.setAutoCreateRowSorter(true);
        jtbDiscount.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtbDiscount.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jtbDiscount.getTableHeader().setReorderingAllowed(false);
        jspTableScroller.setViewportView(jtbDiscount);

        jpDiscountContent.add(jspTableScroller);
        jspTableScroller.setBounds(405, 25, 450, 350);

        jbtRefresh.setBackground(new java.awt.Color(39, 174, 96));
        jbtRefresh.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtRefresh.setForeground(new java.awt.Color(255, 255, 255));
        jbtRefresh.setText("Refresh Table");
        jbtRefresh.setFocusPainted(false);
        jbtRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRefreshActionPerformed(evt);
            }
        });
        jpDiscountContent.add(jbtRefresh);
        jbtRefresh.setBounds(405, 380, 140, 45);

        jbtRemove.setBackground(new java.awt.Color(39, 174, 96));
        jbtRemove.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtRemove.setForeground(new java.awt.Color(255, 255, 255));
        jbtRemove.setText("Remove Item");
        jbtRemove.setFocusPainted(false);
        jbtRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRemoveActionPerformed(evt);
            }
        });
        jpDiscountContent.add(jbtRemove);
        jbtRemove.setBounds(715, 380, 140, 45);

        jlblPromotion.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblPromotion.setText("PROMOTION DISCOUNT RATE");
        jlblPromotion.setToolTipText("");
        jpDiscountContent.add(jlblPromotion);
        jlblPromotion.setBounds(405, 0, 450, 20);
        jpDiscountContent.add(jSeparator1);
        jSeparator1.setBounds(405, 20, 450, 5);

        jpDiscount.add(jpDiscountContent);
        jpDiscountContent.setBounds(5, 5, 860, 430);

        add(jpDiscount);
        jpDiscount.setBounds(0, 20, 870, 440);

        javax.swing.GroupLayout jpDiscountArrowLayout = new javax.swing.GroupLayout(jpDiscountArrow);
        jpDiscountArrow.setLayout(jpDiscountArrowLayout);
        jpDiscountArrowLayout.setHorizontalGroup(
            jpDiscountArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jpDiscountArrowLayout.setVerticalGroup(
            jpDiscountArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        add(jpDiscountArrow);
        jpDiscountArrow.setBounds(540, 0, 150, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRefreshActionPerformed
        populateTableData();
        jtbDiscount.clearSelection();
    }//GEN-LAST:event_jbtRefreshActionPerformed

    private void jbtRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRemoveActionPerformed
        if (jtbDiscount.getSelectedRow() > -1) {
            Object id = jtbDiscount.getModel().getValueAt(jtbDiscount.getSelectedRow(), 0);
            int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to remove this item?", "Remove Item", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.YES_OPTION) {
                String result = RestServer.httpDelete("discount", "" + id);
                try {
                    JSONObject delete = new JSONObject(result);
                    int status = delete.getInt("status");
                    if (status == 200) {
                        populateTableData();
                        Toast.makeText(this, "Item successfully removed.", Toast.LENGTH_SHORT).display();
                    } else {
                        Toast.makeText(this, "Unable to remove now, please try again later.", Toast.LENGTH_SHORT).display();
                    }
                } catch (JSONException ex) {
                }
            }
        } else {
            Toast.makeText(this, "Please make selection first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtRemoveActionPerformed

    private void jbtClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtClearActionPerformed
        jtfDesc.setText("");
        jspRate.setValue(0.1f);
        jdcStartDate.setDate(null);
        jdcExpireDate.setDate(null);
    }//GEN-LAST:event_jbtClearActionPerformed

    private void jbtSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSubmitActionPerformed
        Map<String, Object> data = new HashMap<>();
        if (jtfDesc.getText().isEmpty() || jdcExpireDate.getDate() == null || jdcStartDate.getDate() == null) {
            Toast.makeText(this, "Please fill up all field first.", Toast.LENGTH_LONG).display();
        } else {
            String desc = jtfDesc.getText();
            Object rate = jspRate.getValue();
            Date start = jdcStartDate.getDate();
            Date expire = jdcExpireDate.getDate();
            String startDate = format.format(start);
            String endDate = format.format(expire);
            data.put("desc", desc);
            data.put("rate", rate);
            data.put("start", startDate);
            data.put("end", endDate);
            String result = RestServer.httpPost("discount", data);
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    if (json.getInt("status") == 200) {
                        Toast.makeText(this, "New promotion rate added.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    } else {
                        Toast.makeText(this, "Unable to add new promotion now, try again later.", Toast.LENGTH_SHORT).display();
                    }
                } else {
                    Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
            }
        }
    }//GEN-LAST:event_jbtSubmitActionPerformed

    private void jbtAddDiscountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddDiscountActionPerformed
        cl.show(jpDiscountCard, "addcard");
        jtfMDesc.setText(null);
        jspMRate.setValue(0.1f);
        jdcMExpireDate.setDate(null);
        jdcMStartDate.setDate(null);
    }//GEN-LAST:event_jbtAddDiscountActionPerformed

    private void jbtModifyDiscountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtModifyDiscountActionPerformed
        jtfDesc.setText("");
        jspRate.setValue(0.1f);
        jdcStartDate.setDate(null);
        jdcExpireDate.setDate(null);
        if (jtbDiscount.getSelectedRow() > -1) {
            cl.show(jpDiscountCard, "modifycard");
            did = (Integer) jtbDiscount.getModel().getValueAt(jtbDiscount.getSelectedRow(), 0);
            jtfMDesc.setText(jtbDiscount.getValueAt(jtbDiscount.getSelectedRow(), 0).toString());
            jspMRate.setValue(jtbDiscount.getValueAt(jtbDiscount.getSelectedRow(), 1));
            try {
                jdcMStartDate.setDate(format.parse(jtbDiscount.getValueAt(jtbDiscount.getSelectedRow(), 2).toString()));
                jdcMExpireDate.setDate(format.parse(jtbDiscount.getValueAt(jtbDiscount.getSelectedRow(), 3).toString()));
            } catch (ParseException ex) {
                Logger.getLogger(DiscountPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            Toast.makeText(this, "Please select item from the table first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtModifyDiscountActionPerformed

    private void jbtMClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMClearActionPerformed
        jtfMDesc.setText(null);
        jspMRate.setValue(0.1f);
        jdcMExpireDate.setDate(null);
        jdcMStartDate.setDate(null);
    }//GEN-LAST:event_jbtMClearActionPerformed

    private void jbtMSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMSubmitActionPerformed
        Map<String, Object> data = new HashMap<>();
        if (jtfMDesc.getText().isEmpty() || jdcMExpireDate.getDate() == null || jdcMStartDate.getDate() == null) {
            Toast.makeText(this, "Please fill up all field first.", Toast.LENGTH_LONG).display();
        } else {
            String desc = jtfMDesc.getText();
            Object rate = jspMRate.getValue();
            Date start = jdcMStartDate.getDate();
            Date expire = jdcMExpireDate.getDate();
            String startDate = format.format(start);
            String endDate = format.format(expire);
            data.put("desc", desc);
            data.put("rate", rate);
            data.put("start", startDate);
            data.put("end", endDate);
            String result = RestServer.httpPut("discount", data, "" + did);
            try {
                json = new JSONObject(result);
                boolean error = json.optBoolean("error");
                if (!error) {
                    int statuscode = json.getInt("status");
                    if (statuscode == 200) {
                        Toast.makeText(this, "Item has been updated successfully.", Toast.LENGTH_SHORT).display();
                        populateTableData();
                    } else {
                        Toast.makeText(this, "Unable to update item now, please try again later.", Toast.LENGTH_SHORT).display();
                    }
                } else {
                    Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
                }
            } catch (JSONException ex) {
            }
        }
    }//GEN-LAST:event_jbtMSubmitActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtAddDiscount;
    private javax.swing.JButton jbtClear;
    private javax.swing.JButton jbtMClear;
    private javax.swing.JButton jbtMSubmit;
    private javax.swing.JButton jbtModifyDiscount;
    private javax.swing.JButton jbtRefresh;
    private javax.swing.JButton jbtRemove;
    private javax.swing.JButton jbtSubmit;
    private com.toedter.calendar.JDateChooser jdcExpireDate;
    private com.toedter.calendar.JDateChooser jdcMExpireDate;
    private com.toedter.calendar.JDateChooser jdcMStartDate;
    private com.toedter.calendar.JDateChooser jdcStartDate;
    private javax.swing.JLabel jlblDesc;
    private javax.swing.JLabel jlblExpireDate;
    private javax.swing.JLabel jlblMDesc;
    private javax.swing.JLabel jlblMExpireDate;
    private javax.swing.JLabel jlblMRate;
    private javax.swing.JLabel jlblMStartDate;
    private javax.swing.JLabel jlblPromotion;
    private javax.swing.JLabel jlblRate;
    private javax.swing.JLabel jlblStartDate;
    private javax.swing.JPanel jpAddArrow;
    private javax.swing.JPanel jpAddCard;
    private javax.swing.JPanel jpAddDiscount;
    private javax.swing.JPanel jpDiscount;
    private javax.swing.JPanel jpDiscountArrow;
    private javax.swing.JPanel jpDiscountCard;
    private javax.swing.JPanel jpDiscountContent;
    private javax.swing.JPanel jpModifyArrow;
    private javax.swing.JPanel jpModifyCard;
    private javax.swing.JPanel jpModifyDiscount;
    private javax.swing.JPanel jpNullcard;
    private javax.swing.JPanel jpWorkContent;
    private javax.swing.JSpinner jspMRate;
    private javax.swing.JSpinner jspRate;
    private javax.swing.JScrollPane jspTableScroller;
    private javax.swing.JTable jtbDiscount;
    private javax.swing.JTextField jtfDesc;
    private javax.swing.JTextField jtfMDesc;
    // End of variables declaration//GEN-END:variables
}
